var e = require("../../@babel/runtime/helpers/interopRequireDefault"), t = require("../../@babel/runtime/helpers/createForOfIteratorHelper"), o = e(require("../../@babel/runtime/regenerator")), r = require("../../@babel/runtime/helpers/asyncToGenerator"), a = e(require("../../http/robot")), n = e(require("../../http/info")), i = e(require("../../http/system")), s = require("../../utils/tool"), c = getApp(), u = require("../../http/userApi.js"), d = new a.default(), l = new n.default(), p = new i.default();

Page({
    timeInterval: null,
    data: {
        leftTime: 0,
        orderId: "",
        orderDetail: {},
        buttonText: "",
        isZero: !1,
        couponHistoryId: "",
        discountType: 0,
        computedPriceDetails: {},
        couponList: [],
        noCanUseList: [],
        payDealId: ""
    },
    onLoad: function(e) {
        this.setData({
            orderNo: e.orderNo
        });
    },
    onShow: function() {
        this.timeInterval && clearInterval(this.timeInterval), this.getDetail();
    },
    onUnload: function() {
        clearInterval(this.timeInterval);
    },
    getDetail: function() {
        var e = this;
        return r(o.default.mark(function t() {
            var r;
            return o.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, wx.showLoading({
                        title: "加载中",
                        mask: !0
                    }), t.next = 4, d.robotOrderDetails(e.data.orderNo);

                  case 4:
                    r = t.sent, e.setData({
                        orderDetail: r.data,
                        memberId: r.data.memberId || c.data.loginInfo.memberId
                    }), e.getLeftTime(r.data.robotOverTime), setTimeout(function() {
                        e.getRobotOrderPrice();
                    }, 1e3), t.next = 12;
                    break;

                  case 10:
                    t.prev = 10, t.t0 = t.catch(0);

                  case 12:
                    return t.prev = 12, t.finish(12);

                  case 14:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 10, 12, 14 ] ]);
        }))();
    },
    showCouponDialog: function() {
        this.selectComponent("#couponDialog").showCouponDialog();
    },
    setCouponHistoryId: function(e) {
        e.detail.id ? (this.setData({
            couponHistoryId: e.detail.id,
            discountType: 2
        }), this.selectComponent("#couponDialog").hideCouponDialog()) : this.setData({
            couponHistoryId: "",
            discountType: 0
        }), this.getRobotOrderPrice();
    },
    setIntergal: function(e) {
        e.detail.intergal ? this.setData({
            couponHistoryId: "",
            discountType: 1
        }) : this.setData({
            couponHistoryId: "",
            discountType: 0
        }), this.getRobotOrderPrice();
    },
    getRobotOrderPrice: function() {
        var e = this;
        return r(o.default.mark(function t() {
            var r;
            return o.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, t.next = 3, d.robotOrderComputed({
                        orderNo: e.data.orderNo,
                        memberId: e.data.memberId,
                        discountType: e.data.discountType,
                        couponHistoryId: e.data.couponHistoryId
                    });

                  case 3:
                    r = t.sent, e.setData({
                        computedPriceDetails: r.data,
                        isZero: 0 == r.data.discountAmount
                    }), t.next = 9;
                    break;

                  case 7:
                    t.prev = 7, t.t0 = t.catch(0);

                  case 9:
                    return t.prev = 9, wx.hideLoading(), t.finish(9);

                  case 12:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 7, 9, 12 ] ]);
        }))();
    },
    getRobotOrderCoupon: function() {
        var e = this;
        return r(o.default.mark(function r() {
            var a, n, i, c, u, l, p;
            return o.default.wrap(function(o) {
                for (;;) switch (o.prev = o.next) {
                  case 0:
                    if (0 != e.data.computedPriceDetails.usefulCouponNum) {
                        o.next = 3;
                        break;
                    }
                    return (0, s.toastToPage)("暂无可用优惠券"), o.abrupt("return");

                  case 3:
                    return o.prev = 3, wx.showLoading({
                        title: "加载中",
                        mask: !0
                    }), o.next = 7, d.robotOrderCoupon({
                        memberId: e.data.memberId,
                        orderNo: e.data.orderDetail.orderNo
                    });

                  case 7:
                    a = o.sent, n = t(a.data.couponList);
                    try {
                        for (n.s(); !(i = n.n()).done; ) (c = i.value).collectStartTime = (0, s.formatTime)(c.collectStartTime), 
                        c.collectEndTime = (0, s.formatTime)(c.collectEndTime);
                    } catch (e) {
                        n.e(e);
                    } finally {
                        n.f();
                    }
                    u = t(a.data.noCanUseList);
                    try {
                        for (u.s(); !(l = u.n()).done; ) (p = l.value).collectStartTime = (0, s.formatTime)(p.collectStartTime), 
                        p.collectEndTime = (0, s.formatTime)(p.collectEndTime);
                    } catch (e) {
                        u.e(e);
                    } finally {
                        u.f();
                    }
                    e.setData({
                        couponList: a.data.couponList,
                        noCanUseList: a.data.noCanUseList
                    }), e.showCouponDialog(), o.next = 18;
                    break;

                  case 16:
                    o.prev = 16, o.t0 = o.catch(3);

                  case 18:
                    return o.prev = 18, wx.hideLoading(), o.finish(18);

                  case 21:
                  case "end":
                    return o.stop();
                }
            }, r, null, [ [ 3, 16, 18, 21 ] ]);
        }))();
    },
    tappingPay: function() {
        this.data.leftTime <= 0 ? (0, s.toastToPage)("支付超时，订单已关闭！请刷新机器上的支付码重新扫码支付！") : this.data.isZero ? this.zeroPay() : this.getWxCode();
    },
    zeroPay: function() {
        var e = this;
        return r(o.default.mark(function t() {
            var r;
            return o.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.prev = 0, wx.showLoading({
                        mask: !0,
                        title: "加载中"
                    }), r = {
                        orderNo: e.data.orderDetail.orderNo,
                        couponHistoryId: e.data.couponHistoryId,
                        memberId: e.data.memberId,
                        paymentWay: 1
                    }, t.next = 5, d.zeroPayRobotOrder(r);

                  case 5:
                    t.sent, (0, s.toastToPage)("确认订单成功"), e.toRobotSuccess(), t.next = 12;
                    break;

                  case 10:
                    t.prev = 10, t.t0 = t.catch(0);

                  case 12:
                    return t.prev = 12, wx.hideLoading(), t.finish(12);

                  case 15:
                  case "end":
                    return t.stop();
                }
            }, t, null, [ [ 0, 10, 12, 15 ] ]);
        }))();
    },
    getWxCode: function() {
        var e = this;
        wx.login({
            success: function(t) {
                t.code ? e.pay(t.code) : (0, s.toastToPage)("获取code失败,请重试！" + t.errMsg);
            },
            fail: function(e) {
                (0, s.toastToPage)("获取code失败,请重试！" + e);
            }
        });
    },
    pay: function(e) {
        var t = this;
        return r(o.default.mark(function r() {
            var a, n, i;
            return o.default.wrap(function(o) {
                for (;;) switch (o.prev = o.next) {
                  case 0:
                    return o.prev = 0, wx.showLoading({
                        mask: !0,
                        title: "加载中"
                    }), o.next = 4, l.getOpenId({
                        code: e
                    });

                  case 4:
                    return a = o.sent, n = {
                        orderNo: t.data.orderDetail.orderNo,
                        memberId: t.data.memberId,
                        discountType: t.data.discountType,
                        couponHistoryId: t.data.couponHistoryId,
                        wxOpenId: a.data.openId,
                        paymentWay: 1
                    }, o.next = 8, d.submitRobotOrder(n);

                  case 8:
                    i = o.sent, t.setData({
                        payDealId: i.data.payDealId
                    }), (0, s.callWeChatPay)(i.data.payInfo, t.toRobotSuccess, t.cancelPay), o.next = 15;
                    break;

                  case 13:
                    o.prev = 13, o.t0 = o.catch(0);

                  case 15:
                    return o.prev = 15, wx.hideLoading(), o.finish(15);

                  case 18:
                  case "end":
                    return o.stop();
                }
            }, r, null, [ [ 0, 13, 15, 18 ] ]);
        }))();
    },
    toRobotSuccess: function() {
        var e = this, t = "MB3dRY6WLJhC1kSxHEjspgF3fHrRuB5UXt2_x9gfVpw", o = "y_AzBJRhjemy0g9tmVA366OK-lmxg1P4Qb_CEHFSlTc";
        wx.requestSubscribeMessage({
            tmplIds: [ t, o, "BVtYipN8LQAhbg-3jj9IRebcNsz4RG0gnmcKPeuN0Ng" ],
            success: function(r) {
                console.log("success:", r), "accept" == r[t] && (console.log("pointLoseTempl"), 
                u.subscribeNotice(2).then(function(e) {
                    console.log("pointLoseTempl success:", e);
                }).catch(function(e) {
                    console.log("pointLoseTempl error:", e);
                })), "accept" == r[o] && (console.log("pointAddTempl"), u.sendIntegrationChangeSubscribe({
                    orderType: 2,
                    orderId: e.data.orderDetail.orderId
                }).then(function(e) {
                    console.log("pointAddTempl success:", e);
                }).catch(function(e) {
                    console.log("pointAddTempl error:", e);
                })), "accept" == r["BVtYipN8LQAhbg-3jj9IRebcNsz4RG0gnmcKPeuN0Ng"] && (console.log("newProductTempl"), 
                u.subscribeNotice(4).then(function(e) {
                    console.log("newProductTempl success:", e);
                }).catch(function(e) {
                    console.log("newProductTempl error:", e);
                }));
            },
            fail: function(e) {
                console.log("fail:", e);
            },
            complete: function(e) {
                console.log("complete:", e), c.data.offlinePaySuccess = 1, wx.reLaunch({
                    url: "/pages/index/index"
                });
            }
        });
    },
    cancelPay: function() {
        wx.showLoading({
            mask: !0,
            title: "加载中"
        }), d.cancelRobotOrder(this.data.payDealId).then(function(e) {
            wx.hideLoading(), (0, s.toastToPage)("用户取消支付");
        }).catch(function(e) {
            wx.hideLoading(), (0, s.toastToPage)(e.msg);
        });
    },
    getLeftTime: function(e) {
        var t = this;
        return r(o.default.mark(function r() {
            var a;
            return o.default.wrap(function(o) {
                for (;;) switch (o.prev = o.next) {
                  case 0:
                    return o.t0 = parseInt, o.next = 3, t.getTime();

                  case 3:
                    o.t1 = o.sent, o.t2 = o.t1 / 1e3, a = (0, o.t0)(o.t2), e = parseInt(new Date((0, 
                    s.formatIosTime)(e)).getTime() / 1e3), t.setData({
                        leftTime: e - a
                    }), t.timeInterval = setInterval(function() {
                        var e = t.data.leftTime;
                        e <= 0 || (e--, t.setData({
                            leftTime: e
                        }));
                    }, 1e3);

                  case 9:
                  case "end":
                    return o.stop();
                }
            }, r);
        }))();
    },
    getTime: function() {
        return r(o.default.mark(function e() {
            var t;
            return o.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    return e.next = 2, p.getServerTime();

                  case 2:
                    return t = e.sent, e.abrupt("return", t.data.systemTime);

                  case 4:
                  case "end":
                    return e.stop();
                }
            }, e);
        }))();
    }
});